package framework.attack;

import java.awt.geom.Rectangle2D;

import org.newdawn.slick.Animation;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SpriteSheet;
import org.newdawn.slick.geom.Rectangle;

import framework.AnimationManager;
import framework.BasicFighter;
import framework.Util;
import framework.attack.BasicAttack;

public class AnimationAttack extends BasicAttack<BasicFighter> {
	
	protected Animation attackAnimation;
	
	protected Animation attackAnimationBackward;
	
	protected Animation attackAnimationUsed;
	
	protected SpriteSheet spriteSheet;
	
	protected boolean isJumpingAuthorized;
	
	public AnimationAttack(BasicFighter attacker) {
		AnimationManager n = attacker.getAnimations();
		this.spriteSheet = n.getSpriteSheet();
		Image[] frames = new Image[3];
		frames[0] = this.spriteSheet.getSprite(0, 2);
		frames[1] = this.spriteSheet.getSprite(1, 2);
		frames[2] = this.spriteSheet.getSprite(1, 2);
		this.attackAnimation = new Animation(frames, 150);
		this.attackAnimation.setDuration(0, 150);
		this.attackAnimationBackward = Util.getFlippedAnimation(this.attackAnimation, true, false);
		this.attacker = attacker;
		this.isJumpingAuthorized = false;
	}
	
	@Override
	public void update(GameContainer gc, int delta) {
		super.update(gc, delta);
	}
	
	@Override
	public void draw(Graphics g) {
		BasicFighter a = getAttacker();
		this.attackAnimationUsed.draw(a.getX(), a.getY(), a.getWidth(), a.getHeight());
	}
	
	@Override
	public void trigger() {
		if(!this.isJumpingAuthorized && !getAttacker().isJumping() || this.isJumpingAuthorized) {
			super.trigger();
			this.attackAnimation.restart();
			this.attackAnimationBackward.restart();
			this.attackAnimationUsed = getAttacker().isForward() ? this.attackAnimation : this.attackAnimationBackward;
			this.attackAnimationUsed.setLooping(false);
		}
	}

	@Override
	public int getDuration() {
		return Util.getDuration(attackAnimationUsed);
	}
	
	@Override
	public Rectangle getHitBox() {
		Rectangle hitBox, att;
		att = getAttacker().getHitBox();
		int width = (int) (att.getWidth() * 3);
		if(attackAnimationUsed == attackAnimation) {
			hitBox = new Rectangle(att.getX() +  att.getWidth(), 
					               att.getY() +  att.getHeight() / 3, 
					               width, att.getHeight() / 4);
		} else {
			hitBox = new Rectangle(att.getX() - width, 
					               att.getY() +  att.getHeight() / 3, 
					               width, att.getHeight() / 4);
		}
		return attackAnimationUsed.getFrame() == 1 ?
				hitBox : new Rectangle(-1000, -1000, 0, 0);
	}
	
	@Override
	public boolean isBlocking() {
		return isActive();
	}

	@Override
	public boolean isDrawingAttacker() {
		return isActive();
	}
	
	@Override
	public void onHit(BasicFighter f, Rectangle2D Collision) {
		int forward = f.isForward() ? -1 : 1;
		if (!super.hasHit()) {
			f.setX((int)(f.getX() + getHitBox().getWidth() / 4 * forward));
		}
		super.onHit(f, Collision);
	}

	@Override
	public int getPower() {
		return (currentTime * 10 / getDuration()) + 5;
	}

	@Override
	public void onRelease() {
		if (isActive()) {
			attackAnimationUsed.setAutoUpdate(true);
		}
	}
}
